package com.bwie.service.impl;

import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.ProdImageListMapper;
import com.bwie.mapper.TbItemMapper;
import com.bwie.pojo.ProdImageList;
import com.bwie.pojo.TbItem;
import com.bwie.pojo.TbType;
import com.bwie.service.TbTypeService;
import com.bwie.mapper.TbTypeMapper;
import com.bwie.utils.R;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
* @author wx200
* @description 针对表【tb_type】的数据库操作Service实现
* @createDate 2025-07-03 20:34:44
*/
@Service
public class TbTypeServiceImpl extends ServiceImpl<TbTypeMapper, TbType>
    implements TbTypeService{
    @Autowired
    TbTypeMapper tbTypeMapper;

    @Autowired
    TbItemMapper tbItemMapper;
    @Autowired
    FastFileStorageClient fastFileStorageClient;

    @Autowired
    ProdImageListMapper prodImageListMapper;
    @Override
    public R listType() {
        List<TbType> tbTypes = tbTypeMapper.selectList(null);
        return R.OK(tbTypes);
    }

    @Override
    public R addItemList(TbItem tbItem) {
        tbItem.setLx("9.9%");
        if(tbItem.getItemStatus()==1){
            tbItem.setXmType("车贷");
        }
        if(tbItem.getItemStatus()==2){
            tbItem.setXmType("消费贷");
        }  if(tbItem.getItemStatus()==3){
            tbItem.setXmType("装修贷");
        }  if(tbItem.getItemStatus()==4){
            tbItem.setXmType("信用贷");
        }
        tbItemMapper.insert(tbItem);
        /**
         * 多图片上传
         */
        if(tbItem.getProdImageLists()!=null && tbItem.getProdImageLists().size()>0){
            for (ProdImageList prodImageList : tbItem.getProdImageLists()) {
                prodImageList.setItemId(tbItem.getId());
                prodImageListMapper.insert(prodImageList);
            }
        }
        return R.OK();
    }

    /**
     * 图片上传
     * @param file
     * @return
     */
    @Override
    public R imageUpload(MultipartFile file) throws IOException {
        try {
            String extName = FileUtil.extName(file.getOriginalFilename());
            StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(), extName, null);
            String url = "http://192.168.80.192:8888/"+storePath.getFullPath();
            return R.OK(url);
        } catch (IOException e) {
            e.printStackTrace();
            return R.ERROR(500,"图片上传失败");
        }

    }

    @Override
    public R itemList() {
        List<TbItem> tbItems = tbItemMapper.selectList(null);
        return R.OK(tbItems);
    }
}




